<template>
  <div class="app-container">
    <el-row>
      <!-- 搜索区域 -->
      <el-card style="margin:0 0 5px 5px" v-show="showSearch">
        <el-form ref="queryForm" :inline="true" :model="queryData">
          <el-form-item label="名称" prop="Q_name_S_LK">
            <el-input v-model="queryData.Q_name_S_LK" placeholder="搜索引擎名称" />
          </el-form-item>
          <el-form-item label="地址" prop="Q_url_S_LK">
            <el-input v-model="queryData.Q_url_S_LK" placeholder="搜索引擎地址" />
          </el-form-item>
          <el-form-item class="fr p-r-10">
            <el-button class="filter-item" type="primary" round icon="el-icon-search" @click="handleFilter">
              搜索
            </el-button>
            <el-button class="filter-item m-l-10" type="info" round icon="el-icon-refresh" @click="handleClear">
              清空
            </el-button>
          </el-form-item>
        </el-form>
      </el-card>

      <!-- table区域 -->
      <el-card class="m-l-5">
        <el-row :gutter="10" class="m-b-5">
          <el-col :span="1.5">
            <el-button v-permission="['spider:rule:add']" size="mini" plain type="primary" icon="el-icon-plus" @click="handleInfo()">
              新增
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button v-permission="['spider:rule:delete']" v-if="selectionData.length > 0" size="mini" plain type="danger" icon="el-icon-delete" @click="handleDelete()">
              删除
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button v-permission="['spider:rule:export']" size="mini" plain type="success" icon="el-icon-download" @click="handleDownload()">
              下载
            </el-button>
          </el-col>
          <table-toolbar :showSearch.sync="showSearch" @refresh="getTableData" />
        </el-row>
        <el-table
          ref="rule_table"
          v-loading="tableLoading"
          row-key="id"
          :data="tableData"
          element-loading-text="加载中"
          border
          fit
          highlight-current-row
          show-overflow-tooltip
          height="100px"
          v-el-height-adaptive-table="{bottomOffset: 110}"
          @sort-change="sortChange"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="40" />
          <el-table-column align="center" label="引擎名称" prop="name" sortable width="180" show-overflow-tooltip>
            <template slot-scope="scope">
              <span style="cursor: pointer" @click="handleInfo(scope.row.id)">{{ scope.row.name }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="引擎地址" prop="url" show-overflow-tooltip />
          <el-table-column align="center" label="代理" width="90">
            <template slot-scope="scope">
              <el-tag :type="getDictType('proxy', scope.row.proxy)" v-text="getDictValue('proxy', scope.row.proxy)" />
            </template>
          </el-table-column>
          <el-table-column align="center" label="创建日期" prop="createTime" sortable width="180" show-overflow-tooltip />
          <el-table-column align="center" label="操作" width="160" fixed="right">
            <template slot-scope="scope">
              <el-link type="primary" icon="el-icon-view" @click="handleInfo(scope.row.id)">查看</el-link>
              <el-link v-permission="['spider:rule:delete']" class="m-l-10" type="danger" icon="el-icon-delete" @click="handleDelete(scope.row.id)">删除</el-link>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="tableQuery.total>0" :total="tableQuery.total" :page.sync="tableQuery.page" :limit.sync="tableQuery.limit" @pagination="getTableData" />
      </el-card>
    </el-row>

    <rule-form-dialog v-if="openFormDialog" ref="ruleForm" @confirmForm="handleFilter" />
  </div>
</template>

<script>
import { magnetTable, magnetDelete, magnetExport } from '@/api/tool/search'
import RuleFormDialog from './form/dialog'

export default {
  components: {
    RuleFormDialog
  },
  data() {
    return {
      showSearch: true,
      queryData: {
        Q_name_S_LK: undefined,
        Q_url_S_LK: undefined
      },
      tableLoading: false,
      tableData: [],
      tableQuery: {
        page: 1,
        limit: 20,
        total: 0,
        sort: undefined,
        order: undefined
      },
      selectionData: [],
      optionOfProxy: [],
      openFormDialog: false
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      // 加载下拉菜单
      this.getDictData()
      this.getTableData()
    },
    getDictData() {
      this.$dictGroup('sys_available_type').then(response => {
        this.optionOfProxy = response
      }).catch(e => {})
    },
    getDictType(property, value) {
      return this.$api.getDictElement(this.optionOfProxy, value)[0]
    },
    getDictValue(property, value) {
      return this.$api.getDictElement(this.optionOfProxy, value)[1]
    },
    handleFilter() {
      this.tableQuery.page = 1
      this.getTableData()
    },
    handleClear() {
      this.$refs.queryForm.resetFields()
      this.getTableData()
    },
    getTableData() {
      const query = Object.assign(this.tableQuery, this.queryData)
      this.tableLoading = true
      magnetTable(query).then(response => {
        this.tableData = response.param.data
        this.tableQuery.total = response.param.recordsTotal
        this.tableLoading = false
        this.$refs.rule_table.bodyWrapper.scrollTop = 0
      }).catch(e => {
        this.tableLoading = false
      })
    },
    sortChange(data) {
      const { prop, order } = data
      this.tableQuery.sort = prop
      if (order === 'ascending') {
        this.tableQuery.order = 'asc'
      } else {
        this.tableQuery.order = 'desc'
      }
      this.getTableData()
    },
    handleSelectionChange(val) {
      this.selectionData = val
    },
    handleInfo(id) {
      this.openFormDialog = true
      this.$nextTick(() => {
        this.$refs.ruleForm.init(id)
      })
    },
    handleDelete(id) {
      const ids = this.selectionData.map(function(rule, index) {
        return rule.id
      }).join(',')

      id = id || ids
      if (!id || id.length === 1) {
        this.$modal.msgWarning('请先选中数据项')
        return
      }

      this.$confirm('是否删除所选数据库?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'danger'
      }).then(() => {
        magnetDelete({ ids: id }).then(response => {
          this.handleFilter()
          this.$modal.msgSuccess(response.msg)
        }).catch(e => {})
      }).catch(e => {})
    },
    handleDownload() {
      const query = Object.assign(this.tableQuery, this.queryData)
      this.$modal.loading('下载中...')
      magnetExport(query).then(response => {
        window.location.href = this.$store.state.app.server_api + `${process.env.VUE_APP_MODULE_ADMIN_API}/public/file/download?path=` + encodeURI(response.param) + '&delete=' + true
        this.$modal.closeLoading()
      }).catch(e => { this.$modal.closeLoading() })
    }
  }
}
</script>

<style scoped>
.line{
  text-align: center;
}
</style>
